﻿/*
{
"状态":1,
"名称":"新增-公司资料",
"作者":"陈泽椿",
"创建时间":"2018-06-30",
"功能":"新增-公司资料",
"log": [
	{"date":"06-01" , "dev" :"hzhh" , "content" : ""}
]
}
*/

declare @company_id varchar(100),@company_name varchar(100)
declare @telphone varchar(100),@address varchar(100),@legal_person varchar(100)

set @company_id = '{company_id}'
set @company_name = '{company_name}'
set @telphone ='{telphone}' 
set @address = '{address}'
set @legal_person = '{legal_person}'

--校验信息
if isnull(@company_id,'')=''
begin 
	RAISERROR ('SQL:公司id不允许为空', 16 ,1 ) ; 
	return 
end 

if isnull(@company_name,'')=''
begin 
	RAISERROR ('SQL:公司名称不允许为空', 16 ,1 ) ; 
	return 
end 

if isnull(@telphone,'')=''
begin 
	RAISERROR ('SQL:联系电话不允许为空', 16 ,1 ) ; 
	return 
end 

if isnull(@address,'')=''
begin 
	RAISERROR ('SQL:联系地址不允许为空', 16 ,1 ) ; 
	return 
end 

if isnull(@legal_person,'')=''
begin 
	RAISERROR ('SQL:法人不允许为空', 16 ,1 ) ; 
	return 
end 

IF EXISTS(SELECT * FROM y_company WHERE company_id=@company_id)
begin
	RAISERROR ('SQL:公司id已经存在', 16 ,1 ) ; 
	return 
end  

declare @company_key int
declare @user_key int
declare @role_key int

INSERT INTO [dbo].[y_company]([company_id],[company_name],[telphone],[address],[legal_person],[create_time],[update_time])
VALUES( @company_id  , @company_name , @telphone ,@address ,@legal_person , getdate(), getdate() )

set @company_key=@@IDENTITY
 
insert into [dbo].[y_user]([company_key] ,[user_id] ,[user_name],[update_time],[create_time])
values(@company_key,'admin','管理员',getdate(),getdate())
set @user_key=@@IDENTITY

SELECT Top 1 @role_key=role_key FROM dbo.y_role WHERE role_name='管理员'
 
insert into y_role_user(user_key,role_key) values(@user_key,@role_key)